#include "maintain_tableview.h"

#include "maintain_tablemodel.h"
#include "maintain_progressbardelegate.h"
#include "maintain_buttondelegate.h"

#include <QDebug>
#include <QEvent>

/*
 * Function:    MaintainTableView
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
MaintainTableView::MaintainTableView(QWidget *parent)
    : QTableView(parent)
{
    qDebug() << Q_FUNC_INFO;

    // Initialize the object
    _Initialze();

    // Initialize control's UI
    _InitUi();
}

/*
 * MaintainTableView: ~MaintainTableView
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
MaintainTableView::~MaintainTableView()
{
    qDebug() << Q_FUNC_INFO;

    delete m_pModel;
    m_pModel = NULL;
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void MaintainTableView::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);

    switch (e->type()) {
    case QEvent::LanguageChange: {
        qDebug() << Q_FUNC_INFO;

        QStringList lstHeaders;
        lstHeaders << tr("Item") << tr("Progress") << tr("Error Code") <<tr("Result") << tr("Action");
        m_pModel->setHorizontalHeader(lstHeaders);

        QVector<QStringList> vecRowData = m_pModel->DataVector();
        if(5 == vecRowData.count()) {
            vecRowData[0][0] = tr("CAN");
            vecRowData[1][0] = tr("USB");
            vecRowData[2][0] = tr("Buzzer");
            vecRowData[3][0] = tr("Pipette");
            vecRowData[4][0] = tr("Motion");
            vecRowData[5][0] = tr("Heating");
            m_pModel->setDataVector(vecRowData);
        }

        break;
    }
    default:
        break;
    }
}

/*
 * MaintainTableView: ResetView
 * Description: Reset the value on table view
 * Paramer:     void
 * Return:
 */
void MaintainTableView::ResetView()
{
    qDebug() << Q_FUNC_INFO;

    QVector<QStringList> vecRowData;
    vecRowData.append(QStringList() << tr("CAN") << "0" << "" << "" << "1");
    vecRowData.append(QStringList() << tr("USB") << "0" << "" << "" << "2");
    vecRowData.append(QStringList() << tr("Buzzer") << "0" << "" << "" << "3");
    vecRowData.append(QStringList() << tr("Pipette") << "0" << "" << "" << "4");
    vecRowData.append(QStringList() << tr("Motion") << "0" << "" << "" << "5");
    vecRowData.append(QStringList() << tr("Heating") << "0" << "" << "" << "6");

    m_pModel->setDataVector(vecRowData);
}

/*
 * MaintainTableView: GetValue
 * Description: Get the values under an column
 * Paramer:     int nColIdx - an column index
 * Return:      QStringList - the results
 */
QStringList MaintainTableView::GetValue(int nColIdx)
{
    QStringList lstValue;

    lstValue.clear();
    QVector<QStringList> vecRowData = m_pModel->DataVector();
    for(int i=0; i<vecRowData.size(); i++) {
        lstValue << vecRowData.at(i).at(nColIdx);
    }

    qDebug() << Q_FUNC_INFO << lstValue.size();
    return lstValue;
}

/*
 * Function:    _Initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void MaintainTableView::_Initialze()
{
    qDebug() << Q_FUNC_INFO;

    m_pModel = new MaintainTableModel();

    QStringList lstHeaders;
    lstHeaders << tr("Item") << tr("Progress") << tr("Error Code") << tr("Result") << tr("Action");
    m_pModel->setHorizontalHeader(lstHeaders);

    QVector<QStringList> vecRowData;
    vecRowData.append(QStringList() << tr("CAN") << "0" << "" << "" << "1");
    vecRowData.append(QStringList() << tr("USB") << "0" << "" << "" << "2");
    vecRowData.append(QStringList() << tr("Buzzer") << "0" << "" << "" << "3");
    vecRowData.append(QStringList() << tr("Pipette") << "0" << "" << "" << "4");
    vecRowData.append(QStringList() << tr("Motion") << "0" << "" << "" << "5");
    vecRowData.append(QStringList() << tr("Heating") << "0" << "" << "" << "6");

    m_pModel->setDataVector(vecRowData);

    this->setModel(m_pModel);

    m_pProgressBarDelegate = new MaintainProgressBarDelegate(this);
    m_pButtonDelegate = new MaintainButtonDelegate(this);

    // Signal-slot initialization
    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(m_pButtonDelegate, SIGNAL(signalButtonClicked(int, bool)), this, SIGNAL(signalButtonClicked(int, bool))));
    vecConnect.push_back(QObject::connect(this, SIGNAL(signalProgressFinished(int)), m_pButtonDelegate, SLOT(slotProgressFinished(int))));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_InitUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void MaintainTableView::_InitUi()
{
    qDebug() << Q_FUNC_INFO;

    this->horizontalHeader()->setStretchLastSection(true);
    this->setItemDelegateForColumn(1, m_pProgressBarDelegate);
    this->setItemDelegateForColumn(4, m_pButtonDelegate);
    this->setColumnWidth(0, 200);
    this->setColumnWidth(1, 450);
    this->setColumnWidth(2, 100);
    this->setColumnWidth(3, 100);
    this->setColumnWidth(4, 100);

    for(int i=0; i<this->model()->rowCount(); i++) {
        this->setRowHeight(i, 45);
    }

    emit m_pModel->layoutChanged();
}
